package edu.gyc.producer.consumer;


import java.util.concurrent.Semaphore;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

enum GirlEnum{
    ONE(1,"朱洁静"),TWO(2,"赵雨静"),THREE(3,"柳如是"),
    FOUR(4,"周慧敏"),FIVE(5,"徐睿"),SIX(6,"邱淑贞");
    private int id;
    private String name;

    GirlEnum(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static GirlEnum get(int index) {
      GirlEnum[] girlEnums= GirlEnum.values();
        for (GirlEnum girlEnum : girlEnums) {
            if (girlEnum.getId() == index) {
                return girlEnum;
            }
        }
        return  null;
    }
}

class WhoreClub{

   private static final int NUMS=6;

    private Semaphore semaphore ;
    private boolean[] resources;


    public WhoreClub() {
        this.semaphore = new Semaphore(NUMS,true);
        this.resources = new boolean[NUMS];
        for (int i = 0; i < NUMS; i++) {
            resources[i]=true;
        }
    }

    public void getGirl() {
        try {
            semaphore.acquire();
            int id = getPrepareId();
            System.out.println(Thread.currentThread().getName()+"\t获取了 "+GirlEnum.get(id+1).getName()+"的垂青"+" TIME:"+System.currentTimeMillis());
            System.out.println(Thread.currentThread().getName()+"\t获取了 "+GirlEnum.get(id+1).getName()+"莞式服务进行中...");
            Thread.sleep(50);
            resources[id]=true;
            System.out.println(Thread.currentThread().getName()+"\tbye了我的mm "+GirlEnum.get(id+1).getName()+" TIME:"+System.currentTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            semaphore.release();
        }


    }

    /*  //synchronide和lock版本功能一致
        private synchronized int getPrepareId() {
            int id=-1;
            for (int i = 0; i < NUMS; i++) {
                if (resources[i]) {
                    id=i;
                    resources[i]=false;
                    break;
                }
            }
            return id;
        }


     */
    private Lock lock = new ReentrantLock(true);
private  int getPrepareId() {

    int id = -1;
    try {
        lock.lock();
        for (int i = 0; i < NUMS; i++) {
            if (resources[i]) {
                id = i;
                resources[i] = false;
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        lock.unlock();
    }
    return id;
}

}
public class SemaphoreClub {
    public static void main(String[] args) {

        WhoreClub whoreClub=new WhoreClub();
        for (int i = 1; i < 100; i++) {

            new Thread(() -> {
                whoreClub.getGirl();
            }, String.valueOf(i)).start();

        }
    }
}
